<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 2017/1/5
 * Time: 下午3:35
 */

namespace EWeiLink\JuPai\Http\Controllers;

use EWeiLink\JuPai\Contact;
use EWeiLink\JuPai\Http\Requests\ContactsRequest;
use EWeiLink\JuPai\JpControllerTrait;
use EWeiLink\JuPai\JuPai;
use EWeiLink\JuPai\Services\ContactRepo;
use EWeiLink\JuPai\Services\RelationService;
use EWeiLink\JuPai\Services\SupportService;
use Illuminate\Http\Request;
use EWeiLink\JuPai\Planner;

class ContactsController
{
    use JpControllerTrait;
    protected $repoService;

    public function __construct(ContactRepo $contactRepo)
    {
        $this->repoService = $contactRepo;
    }

    /**
     * 获取联系人列表
     */
    public function index()
    {
        $filter = JuPai::callBridgeHelper('get_filter', []);
        $data = $this->repoService->getContactList($filter);
        return $data;
    }

    public function create()
    {

    }

    public function store()
    {

    }

    public function show($id)
    {
        return $this->repoService->find($id);
    }

    public function edit($id)
    {

    }

    public function update(Request $request, $id)
    {

    }

    public function destroy($id)
    {

    }

    public function unbind($contactId)
    {
        $user = request()->user();
        $contact = Contact::find($contactId);
        if (empty($contact)) {
            return $this->_error('联系人不存在');
        }
        if ($user->isInitUser()) {
            $update = (new RelationService())->bindContactToJP(0, $contact);
            if ($update) {
                return '';
            }
        }
        return $this->_error('该用户不是渠道联系人');
    }

    public function bind(ContactsRequest $request, $contact_id)
    {
        $user = $request->user();
        $customer_id = $request['customerId'];
        $planner = Planner::findByUser($user)->first();
        if (!$planner) {
            return $this->_error('该理财师不存在');
        }

        //联系人合法性检查
        $contact = $planner->contacts()
            ->where('_id', $contact_id)
            ->first();
        if (!$contact) {
            return $this->_error('该联系人不存在');
        }

        //绑定
        $result = (new RelationService())->bindContactToJP($customer_id, $contact);
        if ($result < 0) {
            return $this->_error('该客户无法绑定');
        }
    }

    public function relations(ContactsRequest $request)
    {
        $user = $request->user();
        $sPlannerId = $request['sPlannerId'];
        $tPlannerId = $request['tPlannerId'];
        $contact_id = $request['contact_id'];
        //登录用户权限验证
        if (!$user->isInitUser()) {
            return $this->_error('权限不足');
        }

        //联系人可否转移验证
        $contact = Contact::find($contact_id);
        if (is_null($contact)) {
            return $this->_error('联系人不存在');
        }
        if ((int)$contact->plannerId !== $sPlannerId) {
            return $this->_error('该联系人无法转移');
        }

        //转移
        $result = (new RelationService())
            ->transferRelation($tPlannerId, $contact);

        if (!$result) {
            return $this->_error('关系转移失败');
        }
    }

    /**
     * <title>contactMessage</title>
     * 获取指定联系人的聊天消息
     * @Param: string $contact_id
     */
    public function contactMessage($contact_id) {
        $contact = Contact::find($contact_id);
        if (is_null($contact)) {
            return $this->_error("无对应的联系人");
        }

        $data = $this->repoService->getContactMessage($contact_id);

        return $data;

    }
}